﻿using System;
using System.Linq;
using System.Web.Mvc;
using MiniWeb.Core.Entities;
using MiniWeb.Task;
using MiniWeb.Web.Controllers.Admin.OrderAddressManager.ViewModels;
using WFramework.Mappers;
using WFramework.MvcPager;
using WFramework;
using System.Collections.Generic;
namespace MiniWeb.Web.Controllers.Admin.OrderAddressManager
{

    public class OrderAddressManagerController : BaseController
    {
        private readonly OrderAddressTask _orderaddressTask = new OrderAddressTask();

        public ActionResult List(string account, int pageIndex = 1, int pageSize = 20)
        {
            var pagedList = _orderaddressTask.GetPagedList(account, pageIndex, pageSize);



            var model = new PagedList<OrderAddress>(pagedList.ToList(), pageIndex, pageSize, pagedList.TotalItemCount);

            return View(model);
        }


        public ActionResult Save(int? id)
        {


            var model = new SaveOrderAddress();

            if (id != null)
            {
                var item = _orderaddressTask.GetById((int)id);
                if (item == null)
                    return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                model = EntityMapper.Map<OrderAddress, SaveOrderAddress>(item);
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Save(SaveOrderAddress savemodel)
        {

            if (ModelState.IsValid)
            {

                if (savemodel.ID == null)
                {
                    var model = new OrderAddress
                    {
                        Account = savemodel.Account,
                        RealName = savemodel.RealName,
                        Phone = savemodel.Phone,
                        Address = savemodel.Address,
                        Sheng = savemodel.Sheng,
                        City = savemodel.City,
                        IsDefault = savemodel.IsDefault,
                        CreateDate = DateTime.Now,
                    };
                    _orderaddressTask.Add(model);
                }
                else
                {
                    var model = _orderaddressTask.GetById((int)savemodel.ID);

                    if (model == null)
                        return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                    model.Account = savemodel.Account;
                    model.RealName = savemodel.RealName;
                    model.Phone = savemodel.Phone;
                    model.Address = savemodel.Address;
                    model.Sheng = savemodel.Sheng;
                    model.City = savemodel.City;
                    model.IsDefault = savemodel.IsDefault;


                    _orderaddressTask.Update(model);
                }
                return AlertMsg("保存成功", HttpContext.Request.UrlReferrer.PathAndQuery);

            }

            return View(savemodel);
        }




        #region 删除用户信息 Delete

        [HttpPost]
        public void Delete(int id)
        {
            _orderaddressTask.Delete(id);
        }

        #endregion
    }
}